from django.db import models
from django.contrib.auth.models import User
import datetime

RATING = (
    (0,0),
    (1,.5),
    (2,1),
    (3,1.5),
    (4,2),
    (5,2.5),
    (6,3),
    (7,3.5),
    (8,4),
    (9,4.5),
    (10,5),
)

COMMENT_CHOICES = (
    (1, 'Disabled'),
    (2, 'Frozen'),
    (3, 'Enabled'),
    (4, 'Approval'),
    (5, 'Freeze-Approval'), 
)

MOVIE_RATINGS = (
    (1, 'G'),
    (2, 'PG'), 
    (3, 'PG-13'), 
    (4, 'NC-17'), 
    (5, 'R'),
    (6, 'X'),
)

class MovieManiacAdmin(models.Model):
    allow_comments = models.BooleanField(default=False,)
    add_site_sources = models.ManyToManyField("MovieManiacSiteSource",)
    style_display = models.ForeignKey("MovieManiacStyle",)

    class Meta:
        db_table = 'movie_maniac_admin'
        verbose_name = 'Movie Maniac Admin'
        verbose_name_plural = 'Movie Maniac Admin'

    class Admin:
        pass
    
class MovieManiacSiteSource(models.Model):
    name = models.CharField(max_length=50,)
    is_fetchable = models.BooleanField(default=True,)
    api_key = models.CharField(max_length=250,)

    class Meta:
        db_table = 'movie_maniac_site_sources'
        verbose_name = 'Movie Maniac Site Source'
        verbose_name_plural = 'Movie Maniac Site Sources'

    class Admin:
        pass 

class MovieManiacStyle(models.Model):
    css_styling = models.FileField(upload_to='moviemaniac/css',)
    text_mode = models.BooleanField(default=False,)
    results_per_page = models.IntegerField(default=25,)
    
    class Meta:
        db_table = 'movie_maniac_styles'
        verbose_name = 'Movie Maniac Style'
        verbose_name_plural = 'Movie Maniac Styles'

    class Admin:
        pass

class MovieManiacMovie(models.Model):
    """This model contains the complete list of all movies and its associated content"""
    user = models.ForeignKey(User,)
    #category = models.ManyToManyField("MovieManiacCatagory",)
    medium = models.ManyToManyField("MovieManiacMedium",)
    details = models.ForeignKey("MovieManiacMovieDetail",) 
    comments = models.ForeignKey("MovieManiacCommentsAdmin",)
    viewer_discretion = models.SmallIntegerField(choices=MOVIE_RATINGS,null=True,blank=True)
    site_rating = models.ManyToManyField("MovieManiacSiteRating",)
    user_rating = models.ManyToManyField('MovieManiacUserRating',)
    personal_rating = models.DecimalField(choices=RATING,null=True,max_digits=19,decimal_places=2,)
    related_movies = models.ManyToManyField("MovieManiacRelatedMovie",)
    movie_db_source = models.ForeignKey(MovieManiacSiteSource)
    date_added = models.DateTimeField(default=datetime.datetime.now(),)
    
    class Meta:
        db_table = 'movie_maniac_movies'
        verbose_name = 'Movie Maniac Movies'
        verbose_name_plural = 'Movie Maniac Movies'

class MovieManiacMedium(models.Model):
    Medium_type = models.ForeignKey('MovieManiacMediumType')
    region = models.ForeignKey('MovieManiacRegion')
    covers = models.ManyToManyField('MovieManiacCover')
    
    class Meta:
        db_table = 'movie_maniac_mediums'
        verbose_name = 'Movie Maniac Medium'
        verbose_name_plural = 'Movie Maniac Mediums'    

class MovieManiacMediumType(models.Model):
    medium_type = models.CharField(max_length=50,)
    description = models.CharField(max_length=50,blank=True,)

    class Meta:
        db_table = 'movie_maniac_medium_types'
        verbose_name = 'Movie Maniac Medium Types'
        verbose_name_plural = 'Movie Maniac Medium Types'

class MovieManiacRegion(models.Model):
    region = models.CharField(max_length=50,)
    
    class Meta:
        db_table = 'movie_maniac_regions'
        verbose_name = 'Mmovie Maniac Region'
        verbose_name_plural = 'Movie Maniac Regions'

class MovieManiacCover(models.Model):
    """This Model stores information about our media covers"""
    cover_type = models.ForeignKey("MovieManiacCoverType")
    cover_image = models.ImageField(upload_to='moviemaniac/covers/',)
    
    class Meta:
        db_table = 'movie_maniac_covers'
        verbose_name = 'Movie Maniac Cover'
        verbose_name_plural = 'Movie Maniac Covers'

class MovieManiacCoverType(models.Model):
    """This Model stores the names of the different cover types"""
    cover_type = models.CharField(max_length=15,)
    description = models.CharField(max_length=50, blank=True,)
    
    class Meta:
        db_table = 'movie_maniac_cover_types'
        verbose_name = 'Movie Maniac Cover Type'
        verbose_name_plural = 'Movie Maniac Cover Types'   
                
class MovieManiacBorrower(models.Model):
    """A list of people who are borrowing movies from a particular user"""
    user = models.ForeignKey(User,)
    movie_list = models.ManyToManyField('MovieManiacBorrowersList')
    date_borrowed = models.DateTimeField()
    days_borrowed = models.IntegerField()
    
    class Meta:
        db_table = 'movie_maniac_borrowers'
        verbose_name = 'Movie Maniac Borrower'
        verbose_name_plural = 'Movie Maniac Borrowers'


class MovieManiacBorrowersList(models.Model):
    """A list of movies that users are borrowing"""
    movie_id = models.ForeignKey(MovieManiacMovie)

    class Meta:
        db_table = 'movie_maniac_borrowers_list'
        verbose_name = 'Movie Maniac Borrowers List'
        verbose_name_plural = 'Movie Maniac Borrowers List'
        
class MovieManiacCommentsAdmin(models.Model):
    """
    This model stores whether or not a user
    can comment on a movie.
    """
    comment_switch = models.IntegerField('comments', default=3, choices=COMMENT_CHOICES,help_text=
            '"Disabled" will not display comments for this story.<br />'
            '"Frozen" will display existing comments, but disallow new comments.<br />'
            '"Enabled" will allow new comments.<br />'
            '"Approval" will allow new comments but must be approved first.',
    )
    comments = models.ManyToManyField('MovieManiacComment')

    class Meta:
        db_table = 'movie_maniac_comments_admin'
        verbose_name = 'Movie Maniac Comments Admin'
        verbose_name_plural = 'Movie Maniac Comments Admin'

    class Admin:
       list_display = ('comment_switch',)

class MovieManiacComment(models.Model):
    """
    This model is used to store individual 
    comments for each piece of media.
    """
    user = models.ForeignKey(User,)
    is_approved = models.NullBooleanField(default=True,help_text='Approve this comment',)
    ip_address = models.IPAddressField('IP address', blank=True, null=True)
    comment = models.TextField(blank=False,)
    post_date = models.DateTimeField(blank=False,)

    class Meta:
        db_table = 'movie_maniac_comments'
        verbose_name = 'Movie Maniac Comment'
        verbose_name_plural = 'Movie Maniac Comments'

    class Admin:
       list_display = ('comment','is_approved','post_date',)
       list_filter = ('post_date','is_approved')
       search_fields = ['comment','is_approved']
    
class MovieManiacUserRating(models.Model):
   """
   This model is used to store a users rating 
   on a particular media item
   """
   user = models.ForeignKey(User, unique=True,)
   user_rating = models.DecimalField(choices=RATING,null=True,max_digits=19,decimal_places=2,)

   class Meta:
       db_table = 'movie_maniac_ratings'
       verbose_name = 'Megal User Rating'
       verbose_name_plural = 'Movie Maniac User Ratings'

   class Admin:
       pass   
       
class MovieManiacSiteRating(models.Model):
  """
  This model is used to store a sites 
  rating on a particular movie for example IMDB,Yahoo Movies,etc...  
  """
  name = models.CharField(max_length=25,)
  description = models.TextField(blank=True,)
  rating = models.DecimalField(choices=RATING,null=True,max_digits=19,decimal_places=2,)
  is_main_rating = models.BooleanField(default=False,)

  class Meta:
      db_table = 'movie_maniac_ratings'
      verbose_name = 'Megal User Rating'
      verbose_name_plural = 'Movie Maniac User Ratings'

  class Admin:
      pass
            
class MovieManiacMovieDetail(models.Model):
    ISBN = models.CharField(max_length=13, blank=True,)
    upc_code = models.CharField(blank=True,max_length=12) # Research this
    ean_ucc_13 = models.CharField(blank=True,max_length=13)
    title = models.TextField(max_length=100,)
    alt_title1 = models.TextField(max_length=100,blank=True,)
    alt_title2 = models.TextField(max_length=100,blank=True,)
    thumbnail = models.ImageField(upload_to='moviemaniac/thumbnail/',blank=True,)
    plot = models.TextField(blank=True)
    cast = models.ManyToManyField("MovieManiacCast")
    runtime = models.IntegerField(null=True, blank=True,)
    country = models.CharField(max_length=250, blank=True,)
    genre = models.CharField(max_length=300, blank=True,)
    year = models.IntegerField(null=True, blank=True,)
    director = models.CharField(max_length=25, blank=True,)
    aspect_ratio = models.ForeignKey("MovieManiacAspectRatio")
    audio = models.ManyToManyField("MovieManiacAudio")
    subtitles = models.ManyToManyField("MovieManiacSubtitle")
    NFO = models.FileField(upload_to='moviemaniac/nfo/',blank=True,)
    custom_index =  models.CharField(max_length=10,)
    last_updated = models.DateTimeField(default=datetime.datetime.now())
    
    class Meta:
        db_table = 'movie_maniac_movie_details'
        verbose_name = 'Movie Maniac Movie Detail'
        verbose_name_plural = 'Movie Maniac Movie Details'

    class Admin:
        pass

class MovieManiacRelatedMovie(models.Model): 
    title = models.CharField(max_length=100,)
    # use the OR query to find related movies. if they exist.

    class Meta:
        db_table = 'movie_maniac_related_movies'
        verbose_name = 'Movie Maniac Related Movie'
        verbose_name_plural = 'Movie Maniac Related Movies'

    class Admin:
        pass

class MovieManiacCast(models.Model):        
    actor_name =  models.CharField(max_length=50,)
    real_name = models.CharField(max_length=50,)
    date_of_birth = models.DateField()
    mini_biography = models.TextField(blank=True,)
    awards =  models.TextField(blank=True,)
    # WE should see what other Information is available.

    class Meta:
        db_table = 'movie_maniac_cast'
        verbose_name = 'Movie Maniac Cast'
        verbose_name_plural = 'Movie Maniac Cast'

    class Admin:
        pass
        
class MovieManiacAspectRatio(models.Model):        
    aspect_ratio = models.CharField(max_length=50,)
    description = models.TextField(blank=True,)

    class Meta:
        db_table = 'movie_maniac_aspect_ratios'
        verbose_name = 'Movie Maniac Aspect Ratio'
        verbose_name_plural = 'Movie Maniac Aspect Ratios'

    class Admin:
        pass
      
class MovieManiacSubtitle(models.Model):        
    subtitle = models.CharField(max_length=50,)
    description = models.TextField(blank=True,)

    class Meta:
        db_table = 'movie_maniac_subtitles'
        verbose_name = 'Movie Maniac Subtitle'
        verbose_name_plural = 'Movie Maniac Subtitles'

    class Admin:
        pass

class MovieManiacAudio(models.Model):        
    audio_type = models.CharField(max_length=50,)
    description = models.TextField(blank=True,)

    class Meta:
        db_table = 'movie_maniac_audio'
        verbose_name = 'Movie Maniac Audio'
        verbose_name_plural = 'Movie Maniac Audio'

    class Admin:
        pass